<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/4/16
 * Time: 20:12
 */

namespace app\admin\controller;
use app\admin\model\Word as WordModel;
use think\Db;
use com\wechat\TPWechat;
use think\Config;
/**
 * Class Word
 * @package app\admin\controller
 * 每日单词
 */
class Word extends Admin
{
    /**
     * 主页
     */
    public function index(){
        $map = array(
            'status' => array('egt',0),
        );
        $list = $this->lists('Word',$map);
        int_to_string($list,array(
            'status' => array(0=>"已发布",1=>"已发布"),
            'type' => array(1=>"考研",2=>"专升本",3=>"四六级",4=>"雅思",5=>"托福")
        ));
        $this->assign('list',$list);

        return $this->fetch();
    }

    /**
     * 添加
     */
    public function add(){
        if(IS_POST){
            $data = input('post.');
            if(empty($data['id'])) {
                unset($data['id']);
            }
            $specialModel = new WordModel();
            $result = $specialModel->where(['type' => $data['type'],'day' => $data['day']])->find();
            if (!empty($result)){
                return ['code' => 0,'msg' => '数据重复，注意类型和天数！'];
            }
            $data['create_user'] = $_SESSION['think']['user_auth']['id'];
            $res = $specialModel->validate('Word')->save($data);
            if ($res === false){
                return ['code' => 0,'msg' => $specialModel->getError()];
            }else{
                return $this->success('添加成功',Url('Word/index'));
            }
        }else{
            $this->assign('msg','');
            //获取随机唯一码
            $newModel = new WordModel();
            $code = $newModel->foo();
            $this->assign('code',$code);
            return $this->fetch('edit');
        }
    }

    /**
     * 修改
     */
    public function edit(){
        if(IS_POST){
            $data = input('post.');
            $specialModel = new WordModel();
            $result = $specialModel->where(['type' => $data['type'],'day' => $data['day'],'id' => ['neq',$data['id']]])->find();
            if (!empty($result)){
                return ['code' => 0,'msg' => '数据重复，注意类型和天数！'];
            }
            $model = $specialModel->validate('Word')->save($data,['id'=>input('id')]);
            if($model){
                return $this->success('修改成功!',Url("index"));
            }else{
                return ['code' => 0,'msg' => '暂无更新，修改失败！'];
            }
        }else{
            //根据id获取课程
            $id = input('id');
            if(empty($id)){
                return $this->error("系统错误,不存在该条数据!");
            }else{
                $msg = WordModel::get($id);
                $path = Db::name('picture')->where('id',$msg['front_cover'])->value('path');
                $msg['url'] = "/DDDeducation/public".$path;
                $this->assign('msg',$msg);
            }
            $newModel = new WordModel();
            //获取随机码
            $code = $newModel->foo();
            $this->assign('code',$code);
            return $this->fetch();
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('id');
        if (empty($id)){
            return $this->error("系统参数错误");
        }
        $info = WordModel::where('id',$id)->delete();
        if($info) {
            return $this->success("删除成功");
        }else{
            return $this->error("删除失败");
        }
    }
}